Coverage Report

Created: 2024-12-19 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\api\tools\mod.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
mod error;
30
31
use crate::api::config;
32
use crate::api::core::generator::{Context, Generator};
33
use crate::compiler::util::imports::ImportSolver;
34
use crate::gen::template::loader::TemplateLoader;
35
pub use error::Error;
36
use serde::Deserialize;
37
use std::path::Path;
38
use bp3d_debug::debug;
39
use crate::gen::Generator as Gen;
40
41
pub trait GenTools {
42
    type Params<'a>: Deserialize<'a>;
43
    type Generator: crate::gen::Generator;
44
    type Solver: ImportSolver;
45
46
    fn new_solver() -> Self::Solver;
47
    fn new_generator() -> Self::Generator;
48
    fn generate<'b>(
49
        generator: &'b Generator<'_, Self::Generator>,
50
        context: &mut Context<'b, Self::Solver>,
51
        config: &config::model::Config<Self::Params<'_>>,
52
    ) -> Result<(), Error>;
53
54
2
    fn run(
55
2
        config: &config::model::Config<Self::Params<'_>>,
56
2
        out_dir: impl AsRef<Path>,
57
2
        post_generation: impl FnOnce(&Context<Self::Solver>),
58
2
    ) -> Result<(), Error> {
59
2
        let motherfuckingrust = Self::new_solver();
60
2
        let protocols = config::core::compile(config, &motherfuckingrust)
?0
;
61
2
        let mut loader = TemplateLoader::new();
62
2
        let path = Path::new("./codec/");
63
2
        debug!("Adding codec path {}...", path.canonicalize().unwrap_or_default().display());
64
2
        debug!("Current folder: {}", Path::new(".").canonicalize().unwrap_or_default().display());
65
2
        loader.add_search_path(path);
66
2
        let mut codecs = Self::Generator::get_default_codecs();
67
35
        for 
v30
in
protocols.iter().flat_map(2
|v| v.iter_codecs()
)2
{
68
30
            if !codecs.has(v) {
  Branch (68:16): [True: 0, False: 13]
  Branch (68:16): [Folded - Ignored]
  Branch (68:16): [True: 4, False: 13]
  Branch (68:16): [Folded - Ignored]
69
4
                loader.load(String::from(v)).map_err(Error::TemplateLoader)
?0
;
70
26
            }
71
        }
72
35
        for 
v30
in
protocols.iter().flat_map(2
|v| v.iter_codecs()
)2
{
73
30
            if !codecs.has(v) {
  Branch (73:16): [True: 0, False: 13]
  Branch (73:16): [Folded - Ignored]
  Branch (73:16): [True: 2, False: 15]
  Branch (73:16): [Folded - Ignored]
74
2
                let template = loader.compile(v, &codecs).map_err(Error::TemplateLoader)
?0
;
75
2
                codecs.insert(v, template);
76
28
            }
77
        }
78
2
        let mut generator = Generator::new(out_dir.as_ref(), codecs, Self::new_generator());
79
2
        if let Some(
file_header0
) = config.package.file_header {
  Branch (79:16): [True: 0, False: 1]
  Branch (79:16): [Folded - Ignored]
  Branch (79:16): [True: 0, False: 1]
  Branch (79:16): [Folded - Ignored]
80
0
            generator.set_file_header(file_header);
81
2
        }
82
2
        let mut context = Context::new(&protocols);
83
2
        Self::generate(&generator, &mut context, config)
?0
;
84
2
        post_generation(&context);
85
2
        Ok(())
86
2
    }
87
88
2
    fn run_string(
89
2
        config: impl AsRef<str>,
90
2
        out_dir: impl AsRef<Path>,
91
2
        post_generation: impl FnOnce(&Context<Self::Solver>),
92
2
    ) -> Result<(), Error> {
93
2
        let config = config::core::parse::<Self::Params<'_>>(config.as_ref()).map_err(Error::Config)
?0
;
94
2
        Self::run(&config, out_dir, post_generation)
95
2
    }
96
97
2
    fn run_file(
98
2
        config_file: impl AsRef<Path>,
99
2
        out_dir: impl AsRef<Path>,
100
2
        post_generation: impl FnOnce(&Context<Self::Solver>),
101
2
    ) -> Result<(), Error> {
102
2
        let str = std::fs::read_to_string(config_file).map_err(Error::Io)
?0
;
103
2
        Self::run_string(str, out_dir, post_generation)
104
2
    }
105
}
106
107
#[cfg(feature = "gen-rust")]
108
mod rust;
109
#[cfg(feature = "gen-swift")]
110
mod swift;
111
112
#[cfg(feature = "gen-rust")]
113
pub use rust::Rust;
114
115
#[cfg(feature = "gen-swift")]
116
pub use swift::Swift;